package mm.chenchen.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

    public final static String POUND_SIGN = "#";
    public final static String DOLLAR_SIGN = "$";

    public static String getColumnTypeNumber(String str) {
        if (str.indexOf("(") == -1) {
            return "";
        } else {
            return str.substring(str.indexOf("("), str.indexOf(")") + 1);
        }
    }

    public static String getColumnTypeNumberNoBrackets(String str) {
        if (str.indexOf("(") == -1) {
            return "";
        } else {
            return str.substring(str.indexOf("(") + 1, str.indexOf(")"));
        }
    }

    public static String getSubString(String str) {
        if (str.indexOf("#") == -1) {
            return "";
        } else {
            return str.substring(str.indexOf("#"), str.indexOf("}") + 1);
        }
    }

    public static String getLastSubString(String str) {
        if (str.lastIndexOf("#") == -1) {
            return "";
        } else {
            return str.substring(str.lastIndexOf("#"), str.lastIndexOf("}") + 1);
        }
    }

    public static String get(String str) {
        if (str.indexOf("{") == -1) {
            return "";
        } else {
            return str.substring(str.indexOf("{") + 1, str.indexOf("}"));
        }
    }

//    public static String lineFeddRelpace(String str) {
//
//    }

    public static String getColumnType(String str) {
        if (str.indexOf("(") == -1) {
            return str.trim().toUpperCase();
        } else {
            return str.substring(0, str.indexOf("(")).trim().toUpperCase();
        }
    }

    public static String participateAll(String sql, List<String> placeholders, List<String> values) {
        if (placeholders != null && values != null && placeholders.size() == values.size()) {
            for (int i = 0; i < placeholders.size(); i++) {
                StringBuffer sign = new StringBuffer("\\$\\{");
                sign.append(placeholders.get(i));
                sign.append("}");
                sql = sql.replaceAll(sign.toString(), values.get(i));
            }
        }
        return sql;
    }

//    public static String participate(String sql, String placeholder, String value) {
//        StringBuffer sign = new StringBuffer("\\$\\{");
//        sign.append(placeholder);
//        sign.append("}");
//        String result = sql.replaceAll(sign.toString(), value);
//        return result;
//    }

    public static String participate(String symbol, String sql, String placeholder, String value) {
        StringBuffer sign = new StringBuffer("\\" + symbol + "\\{");
        sign.append(placeholder);
        sign.append("}");
        String result = sql.replaceAll(sign.toString(), value);
        return result;
    }

    public static String replaceIgnoreCase(String symbol, String sql, String placeholder, String value) {
        StringBuffer sign = new StringBuffer("\\" + symbol + "\\{");
        sign.append(placeholder);
        sign.append("}");
        String result = sql.replaceAll("(?i)" + sign.toString(), value);
        return result;
    }

    public static String replaceFirst(String symbol, String sql, String placeholder, String value) {
        StringBuffer sign = new StringBuffer("\\" + symbol + "\\{");
        sign.append(placeholder);
        sign.append("}");
        String result = sql.replaceFirst(sign.toString(), value);
        return result;
    }

    public static String replaceAllIgnoreCase(String symbol, String sql, Map<String, String> map) {
        for (Map.Entry<String, String> m : map.entrySet()) {
            StringBuffer sign = new StringBuffer("\\" + symbol + "\\{");
            sign.append(m.getKey());
            sign.append("}");
            sql = sql.replaceAll("(?i)" + sign.toString(), m.getValue() == null ? "" : handleEscapeCharacter(m.getValue()));
        }
        return sql;
    }

    //将参数中的单引号转换为中文单引号,2021-6-23只有'#{xxx}' 的变量才需要处理
    public static String replaceAllIgnoreCaseHandleSQ(String symbol, String sql, Map<String, String> map) {
        for (Map.Entry<String, String> m : map.entrySet()) {
            StringBuffer sign = new StringBuffer("\\" + symbol + "\\{");
            sign.append(m.getKey());
            sign.append("}");
            String tmpStr = m.getValue();
            if (sql.indexOf("'#{" + m.getKey() + "}'") != -1) {
                tmpStr = tmpStr != null ? tmpStr.replaceAll("'", "‘") : "";
            }
            sql = sql.replaceAll("(?i)" + sign.toString(), tmpStr == null ? "" : handleEscapeCharacter(tmpStr));
        }
        return sql;
    }


    public static String getIntermediate(String str, String from, String end) {

        if (str.indexOf(from) == -1 || str.indexOf(end) == -1) {
            return "";
        } else {
            return str.substring(str.indexOf(from) + 1, str.indexOf(end));
        }
    }

    /**
     * 转义特殊字符
     *
     * @param str
     * @return
     */
    public static String handleEscapeCharacter(String str) {
        String regEx = "[#${}]";
        Pattern p = Pattern.compile(regEx);
        Matcher m1 = p.matcher(str);
        if (m1.find()) {
            CharSequence cs = str;
            int j = 0;
            for (int i = 0; i < cs.length(); i++) {
                String temp = String.valueOf(cs.charAt(i));
                Matcher m2 = p.matcher(temp);
                if (m2.find()) {
                    StringBuilder sb = new StringBuilder(str);
                    str = sb.insert(j, "\\").toString();
                    j++;
                }
                j++; //转义完成后str的长度增1
            }
        }
        return str;
    }

    public static String stringToMD5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有这个md5算法！");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }
}
